'''
Created on 2009-11-28

@author: madlee
'''

from os.path import abspath, getmtime, exists


class DocumentType:
    def __init__(self):
        pass
    
    def newdoc(self, filename):
        raise NotImplementedError()
    
class Document:
    def __init__(self, type, filename=None):
        self.__type = type
        self.__filename = abspath(filename)
        self.__lat = None

    @property
    def filename(self):
        '''return the filename of this document'''
        return self.__filename
    
    @property
    def fileset(self):
        '''return a set of file of this document'''
        return [self.filename] if self.filename is not None and exists(self.filename) else []
    
    @property
    def views(self):
        raise NotImplementedError()
    
    @property
    def icon(self):
        raise NotImplementedError()

    def update_views(self, *argv, **kwargs):
        lmt = self.lmt
        for i in self.views:
            if i.lat < lmt:
                i.update(*argv, **kwargs)
        
    @property    
    def lmt(self):
        fileset = self.fileset
        try:
            lmt = getmtime(fileset[0])
            for i in fileset[1:]:
                lmtI = getmtime(i)
                if lmtI > lmt:
                    lmt = lmtI
            return lmt
        except IndexError:
            return None
        
    @property
    def lat(self):
        return self.__lat
            
    def refresh(self):
        for i in self.views:
            i.refresh()
        
class View:
    def __init__(self, doc):
        self.__document = doc
        
    @property
    def document(self):
        return self.__document
    
    def refresh(self):
        raise NotImplementedError()
    
    @property
    def window(self):
        raise NotImplementedError()
    
    @property
    def toolbar(self):
        raise NotImplementedError()

    @property
    def label(self):
        raise NotImplementedError()
    
    @property
    def icon(self):
        raise NotImplementedError()
    
class TextView(View):
    def __init__(self, doc):
        View.__init__(self, doc)
        
    def get_label(self):
        return TEXT_VIEW_LABEL
